<?php

namespace My\Controller;

use Utuo\BackgroundController;

class SubscribeController extends BackgroundController {
	
	public function _initialize() {
		$this->setControllerTitle ( "预约管理" );
		$this->setActionTitle ( array (
				'index' => '预约活动列表',
				'list' => '预约用户列表',
				'modify' => '预约编辑',
				'add' => '预约活动添加',
				'recycle' => '回收站',
				'userList' => '预约用户列表'
		) );
		$this->setActionNav ( array (
				'index',
				'add',
				'recycle',
		) );
	}
	
	// 预约添加
	public function addAction() {
		$this->display ();
	}
	
	// 添加
	public function addPostAction() {
		$d = D ( 'Subscribe' );
		if ($d->create ()) {
			$d->cid = $this->aid;
			if ($d->add ()) {
				$this->ajaxSuccess ( '添加预约活动成功' );
			} else {
				$this->ajaxError ( $d->getError() );
			}
		} else {
			$this->ajaxError ( '创建数据失败' );
		}
	}
	
	// 预约活动列表
	public function indexAction() {
		$this->getModuleSubscribeList ( 0 );
	}
	
	// 修改预约活动页面
	public function modifyAction() {
		$id = I ( 'id', 0, 'intval' );
		$d = D ( 'Subscribe' );
		$moduleSubscribe = D ( 'Subscribe' )->getById ( $id );
		$this->checkAdmin ( $moduleSubscribe ['cid'] );
		$moduleSubscribe ['content'] = htmlspecialchars_decode ( $moduleSubscribe ['content'] );
		/*自选项*/
		$items = explode(',', $moduleSubscribe['items']);
		$checkboxs = "";
		foreach ($d->items as $k => $v){
			$title = $v;
			!is_array($v) or $title = $v['title'];
			if(in_array($k, $items)){
				$checkboxs.="<label class=\"checkbox-inline\"><input type=\"checkbox\" value=\"{$k}\" checked='checked' />{$title}</label>";
			}else{
				$checkboxs.="<label class=\"checkbox-inline\"><input type=\"checkbox\" value=\"{$k}\" />{$title}</label>";
			}
		}
		$moduleSubscribe ['itemsCheckbox'] = $checkboxs;
		$moduleSubscribe ['url'] = $this->getUrl($id);
		/*页面输出*/
		$this->assign ( 'moduleSubscribe', $moduleSubscribe );
		$this->display ();
	}
	
	// 提交预约活动修改
	public function modifyPostAction() {
		$d = D ( 'Subscribe' );
		if ($d->create ()) {
			$this->checkAdmin ( null, $d, "cid" );
			$d->cid = $this->aid;
			if (false !== $d->save ()) {
				$this->ajaxSuccess ( '修改成功' );
			} else {
				$this->ajaxError ( $d->getError () );
			}
		} else {
			$this->ajaxError ( $d->getError () );
		}
	}
	
	// 移除菜单
	public function removePostAction() {
		$this->ajaxRemove ();
	}
	
	// 恢复菜单
	public function restorePostAction() {
		$this->ajaxRestore ();
	}
	
	// 彻底删除
	public function deletePostAction() {
		$this->ajaxDelete ();
	}
	
	public function setStatusPostAction() {
		$this->ajaxSetStatus("Subscribe");
	}
	
	// 回收站
	public function recycleAction() {
		$this->getModuleSubscribeList ( 1 );
	}
	
	// 活动预约用户列表
	public function userListAction() {
		$vid = I('vid', 0, 'intval');
		$page = $this->getPage ();
		$pagesize = $this->getPageSize ();
		$d = D ( 'SubscribeUser' );
		$condition = array (
			'subscribe_id' => $vid,
		);
		$moduleSubscribeUserLists = $d->where ( $condition )->limit ( $pagesize )->page ( $page )->order ( 'id DESC' )->select ();
		$moduleSubscribeUserCount = $d->where ( $condition )->count ();
		$this->assign ( 'moduleSubscribeUserLists', $moduleSubscribeUserLists );
		$this->showPage ( $moduleSubscribeUserCount, $pagesize );
		$this->display ();
	}
	
	// 彻底删除预约用户
	public function deleteUserPostAction() {
		$this->checkAdmin(null, M('SubscribeUser'),'cid');
		$this->ajaxDelete ('SubscribeUser');
	}
	
	
	// 获取预约活动列表
	private function getModuleSubscribeList($is_deleted) {
		$page = $this->getPage ();
		$pagesize = $this->getPageSize ();
		$d = D ( 'Subscribe' );
		$condition = array (
				'is_deleted' => $is_deleted,
				'cid' => $this->aid 
		);
		$moduleSubscribeLists = $d->where ( $condition )->limit ( $pagesize )->page ( $page )->order ( 'id DESC' )->select ();
		$moduleSubscribeCount = $d->where ( $condition )->count ();
		foreach ($moduleSubscribeLists as &$v) {
			$v['url'] = $this->getUrl($v['id']);
		}
		$this->assign ( 'moduleSubscribeLists', $moduleSubscribeLists );
		$this->showPage ( $moduleSubscribeCount, $pagesize );
		$this->display ();
	}
	
	private function getUrl($id) {
		return url('Home','Subscribe','show', "id={$id}&sid={$this->aid}", true);
	}
}